package com.bytedance.frameworks.baselib.network.http;

import android.content.Context;
import android.net.Uri;
import android.webkit.CookieManager;

import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.connectionclass.ConnectionClassManager;
import com.bytedance.ttnet.a.NetChannelSelect;
import com.bytedance.ttnet.b.HttpRequestInfo;
import com.bytedance.ttnet.b.RequestParams;
import com.ss.android.common.app.AbsApplication;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.applog.NetUtil;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.newmedia.BaseApplication;

import org.json.JSONObject;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;

public class AppConfig {
    private static final Object h;
    private static RxBytesSampleController a;
    private static boolean b;
    private static String c;
    private static d$c d;
    private static a e;
    private static e f;
    private static volatile boolean g;
    private static volatile int i;
    private static b j;

    static {
        b = true;
        c = null;
        g = false;
        h = new Object();
        i = -1;
    }

    public AppConfig() {

    }

    public static void a(String arg1, Throwable arg2) {
        if (!android.support.design.a.isTextEmpty(arg1)) {
        }
    }

    public static void a(long costTimeMS, long newOkhttp3ClientCallTs, String arg20, String arg21, BaseRequestInfo arg22, Throwable arg23) {
        int v5_2;
        Context v4_4;
        com.bytedance.ttnet.a.AppConfig.HttpsFilter v2_7;
        int v2_6;
        Class v7;
        Context v6;
        NetChannelSelect v5_1;
        boolean v2_4;
        com.bytedance.ttnet.a.AppConfig v3_1;
        String v2_2;
        PrintWriter v3;
        StringWriter v2_1;
        JSONObject v10 = null;
        int v9 = 0;
        String[] v11 = new String[0];
        if (!android.support.design.a.isTextEmpty(arg20) && (arg23 != null && AppConfig.f != null)) {
            try {
                v11 = new String[1];
                v9 = android.support.design.a.a(arg23, v11);
                v10 = new JSONObject();
                if (arg23 == null) {
//                    goto label_46;
                } else if (android.support.design.a.isTextEmpty(arg23.getClass().getName())) {
//                    goto label_46;
                } else {
                    v10.put("ex_name", arg23.getClass().getName());
                    if (v9 != 1) {
//                    goto label_46;
                    } else if (!com.bytedance.article.common.a.g.a("ex_message_open")) {
//                    goto label_46;
                    } else {
                        String v5 = null;
                        PrintWriter v4 = null;
                        v2_1 = new StringWriter();
                        v3 = new PrintWriter(((Writer) v2_1));
                        arg23.printStackTrace(v3);
                        Throwable v4_1 = arg23.getCause();
                        if (v4_1 != null) {
                            v4_1.printStackTrace(v3);
                            v4_1 = v4_1.getCause();
                            if (v4_1 != null) {
                                v4_1.printStackTrace(v3);
                            }
                        }

                        v2_2 = v2_1.toString();
                        v3.close();
                        if (!android.support.design.a.isTextEmpty(v2_2)) {
                            v10.put("ex_message", v2_2);
                        }
                    }
                }
                label_46:
                if ((android.support.design.a.isTextEmpty(v11[0])) && arg22 != null) {
                    v11[0] = arg22.timingRemoteIP;
                    if (arg22.requestContext != null) {
                        RequestParams eVar = (RequestParams) arg22.requestContext;
                        if (eVar.cdn_request_num > 0) {
                            v10.put("index", eVar.cdn_request_num);
                        }

                        if (eVar.https_to_http <= 0) {
//                            goto label_76;
                        } else {
                            v10.put("httpIndex", eVar.https_to_http);
                        }
                    }
                }

//                label_76:
                BaseApplication.a((HttpRequestInfo) arg22, v10);
                NetworkUtils.getNetworkType(BaseApplication.getApplication().getApplicationContext()).getValue();
                if (v9 < 400 && v9 >= 200) {
//                    goto label_178;
                } else {

                    if (Logger.debug()) {
                        Logger.d("AppConfig", "status = " + v9 + " tr = " + arg23.getMessage());
                    }

                    v3_1 = com.bytedance.ttnet.a.AppConfig.getInstance(AbsApplication.getApplication());
                    if (android.support.design.a.isTextEmpty(arg20)) {
//                    goto label_178;
                    } else {
                        if (Logger.debug()) {
                            Logger.d("AppConfig", "onRequestErr url = " + arg20);
                        }

                        Uri v4_2 = Uri.parse(arg20);
                        if (v3_1.mNetChannelSelect == null) {
//                    goto label_128;
                        } else {
                            NetChannelSelect v2_3 = v3_1.mNetChannelSelect;
                            if (v4_2 != null) {
                                String v5 = v4_2.getHost();
                                if (v2_3.e != null) {
                                    v2_4 = v2_3.e.containsKey(v5);
                                } else {
                                    v2_4 = false;
                                }
                            } else {
                                v2_4 = false;
                            }

//                label_123:
                            if (!v2_4) {
//                    goto label_128;
                            } else {
                                v5_1 = v3_1.mNetChannelSelect;
                                v6 = v3_1.mContext;
                                if (v6 == null) {
//                    goto label_128;
                                } else if (v4_2 != null) {
                                    v7 = NetChannelSelect.class;
                                    synchronized (v7) {
                                        if (v5_1.f != null) {
                                            NetChannelSelect.g$a v2_5 = v5_1.f;
                                            if (v2_5.a != null && (v2_5.a.equals(v4_2.getHost())) && v2_5.j != null && (v2_5.j.equals(v4_2.getScheme()))) {
                                                ++v2_5.k;
                                            }

                                            long v12 = System.currentTimeMillis();
                                            if (v5_1.f.k >  NetChannelSelect.a(((NetChannelSelect.g$a)v5_1.f).l)) {
                                                v2_6 = 1;
                                            } else {
                                                v2_6 = 0;
                                            }

                                            if (v2_6 != 0 && v12 - v5_1.d > (((long) (v5_1.b * 1000)))) {
                                                if (Logger.debug()) {
                                                    Logger.d("NetChannelSelect", "select from addErrCount");
                                                }
                                                v5_1.a(v6, "onErr");
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (!arg20.startsWith("https")) {

                        } else {
                            v2_2 = arg20.replaceFirst("https", "http");
                            synchronized (v3_1) {
                                try {
                                    com.bytedance.ttnet.a.AppConfig.HttpsFilter[] v4_3 = new com.bytedance.ttnet.a.AppConfig.HttpsFilter[1];
                                    if (!v3_1.isHttpUrlMatch(v2_2, v4_3, false)) {
                                    } else {
                                        v2_7 = v4_3[0];
                                        v4_4 = v3_1.mContext;
                                        v5_2 = v2_7.errCount + 1;
                                        if (v2_7.errCount > com.bytedance.ttnet.a.AppConfig.o()) {
                                        } else if (v5_2 <= com.bytedance.ttnet.a.AppConfig.o()) {
                                        } else if (v4_4 == null) {
                                        } else {
                                            com.bytedance.ttnet.cc.a().a(v4_4, "https", "https2http");
                                        }
                                        v2_7.errCount = v5_2;
                                        if (Logger.debug()) {
                                            Logger.w("AppConfig", "addHttpsUrlErrCount urlRegex = " + v2_7.urlRegex + " errCount = " + v2_7.errCount);
                                        }
                                        if (v2_7 == null) {
                                        } else {
                                            new com.bytedance.ttnet.a.e(v3_1, "SaveHttpsFilters-Thread", v2_7).start();
                                        }
                                    }
                                } catch (Throwable v4_1) {
                                    v4_1.printStackTrace();
                                }
                            }
                        }
                    }
                }
            } catch (Throwable v2) {
                v2.printStackTrace();
            }

            label_178:
            if (arg22 != null) {

            }
            com.bytedance.article.common.a.g.a(costTimeMS, newOkhttp3ClientCallTs, arg20, v11[0], arg21, v9, v10);
            com.bytedance.article.common.a.g.b(costTimeMS, newOkhttp3ClientCallTs, arg20, v11[0], arg21, v9, v10);
            return;
        }
    }

    public static void a(long arg12, long arg14, String arg16, String arg17, BaseRequestInfo requestInfo) {
        e v2 = f;
        if (!android.support.design.a.isTextEmpty(arg16) && arg12 > 0 && v2 != null) {
            try {
                String[] v3 = new String[1];
                JSONObject v10 = new JSONObject();
                if (arg16.contains("&config_retry=b")) {
                    v10.put("log_config_retry", 1);
                }

                if ((android.support.design.a.isTextEmpty(v3[0])) && requestInfo != null) {
                    v3[0] = requestInfo.timingRemoteIP;
                    if (requestInfo.requestContext != null) {
                        if (((RequestParams) requestInfo.requestContext).cdn_request_num > 0) {
                            v10.put("index", ((RequestParams) requestInfo.requestContext).cdn_request_num);
                        }

                        if (((RequestParams) requestInfo.requestContext).https_to_http > 0) {
                            v10.put("httpIndex", ((RequestParams) requestInfo.requestContext).https_to_http);
                        }
                    }
                }

                BaseApplication.a((HttpRequestInfo) requestInfo, v10);
                NetworkUtils.getNetworkType(BaseApplication.getApplication().getApplicationContext()).getValue();
                if (requestInfo != null) {
                }

                com.bytedance.article.common.a.g.b(arg12, arg14, arg16, v3[0], arg17, 200, v10);
            } catch (Throwable v2_1) {
            }
        }
    }

    public static RxBytesSampleController getRxBytesSampleController() {
        return a;
    }

    public static String a(String arg1, BaseRequestContext arg2) {
        if (!android.support.design.a.isTextEmpty(arg1)) {
            b v0 = j;
            if (v0 != null) {
                arg1 = v0.a(arg1, arg2);
            }
        }
        return arg1;
    }

    public static void a(int arg0) {
        i = arg0;
    }

    public static void a(a arg0) {
        e = arg0;
    }

    public static void a(b arg0) {
        j = arg0;
    }

    public static void a(d$c arg0) {
        d = arg0;
    }

    public static void a(RxBytesSampleController arg0) {
        a = arg0;
    }

    public static void a(e arg0) {
        f = arg0;
    }

    public static void a(String arg6) {
        int v0 = 0;
        if (arg6 != null) {
            try {
                if (arg6.length() <= 0) {
                    c = arg6;
                    return;
                }

                char[] v2 = arg6.toCharArray();
                int v3 = v2.length;
                int v1;
                for (v1 = 0; v1 < v3; ++v1) {
                    if (v2[v1] < 32 || v2[v1] > 126) {
                        v2[v1] = '?';
                        v0 = 1;
                    }
                }

                if (v0 == 0) {
                    c = arg6;
                    return;
                }

                arg6 = new String(v2);
            } catch (Exception v0_1) {
            }
        }
        c = arg6;
    }

    public static void a(Map<String,String> queryMap) {
        if (e != null) {
            NetUtil.a(queryMap, true);
        }
    }

    public static void a(boolean arg0) {
        b = arg0;
    }

    public static void b(String text) {
        android.support.design.a.isTextEmpty(text);
    }

    public static int getConnectTimeout() {
        int v0 = 15000;
        if (!b) {
            return v0;
        }

        try {
            switch (com.bytedance.frameworks.baselib.network.http.e.a[ConnectionClassManager.Holder.sInstance.a().ordinal()]) {
                case 1: {
                    return 45000;
                }
                case 2: {
                    return 30000;
                }
            }
        } catch (Throwable v1) {
            v1.printStackTrace();
            return v0;
        }

        return v0;
    }

    public static void b(boolean arg2) {
        Object v1 = h;
        synchronized (v1) {
            g = arg2;
        }
    }

    public static int getReadWriteTimeout() {
        int v0 = 15000;
        if (!b) {
            return v0;
        }

        try {
            switch (com.bytedance.frameworks.baselib.network.http.e.a[ConnectionClassManager.Holder.sInstance.a().ordinal()]) {
                case 1: {
                    return 45000;
                }
                case 2: {
                    return 30000;
                }
            }
        } catch (Throwable v1) {
            v1.printStackTrace();
            return v0;
        }

        return v0;
    }

    public static String c(String arg1) {
        return a(arg1, (BaseRequestContext) null);
    }

    public static String getUserAgent() {
        return c;
    }

    public static String d(String arg1) {
        if (e != null) {
            arg1 = AppLog.a(arg1, true);
        }

        return arg1;
    }

    public static d$c e() {
        return d;
    }

    public static CookieManager f() {
        CookieManager v0_2 = null;
        Object v1 = h;
        synchronized (v1) {
            if (g) {
//                goto label_8;
            } else {
                long v2 = 1500;
                try {
                    Thread.sleep(v2);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                g = true;
            }
        }

        if (e != null) {
            AppLog.v();
        }

        try {
            v0_2 = CookieManager.getInstance();
            if (v0_2.acceptCookie()) {
                return v0_2;
            }

            v0_2.setAcceptCookie(true);
        } catch (Throwable v1_1) {

        }

        return v0_2;
    }

    public static boolean g() {
        return i != 0;
    }

    public static b h() {
        return j;
    }

    public interface b<T extends BaseRequestContext> {
        List<InetAddress> a(String arg1);

        String a(String arg1, T arg2);
    }

    public interface d$c {
        String a();

        void a(List<String> arg1);
    }

    public interface RxBytesSampleController {
        boolean shouldSampleNet(String requestUrl);
    }

    public static final class a {
        public a() {

        }
    }

    public static final class e {
        public e() {

        }
    }
}

